package screen.component
{
	import com.adobe.serialization.json.JSON;
	
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.text.TextField;
	import flash.utils.Timer;
	import flash.utils.clearInterval;
	import flash.utils.clearTimeout;
	import flash.utils.setInterval;
	import flash.utils.setTimeout;
	
	import helper.Reward;
	import helper.Services;
	import helper.ValueObject;
	
	import screen.SacombankScreen;


	public class Home extends UI
	{
		private const _TASK_FOLDER:String ="../assets/avatar/task/";
		private const TOTAL_TASK:Number =6;
		private const GIRL_MC:String="girlMc";
		private var _bg:MovieClip;
		private var _get_task_loader:URLLoader;
		private var _do_task_loader:URLLoader;
		private var _get_question_loader:URLLoader;
		private var _do_question_loader:URLLoader;
		private var _service:Services;
		private var _timer:Timer;
		private var _task_list_arr:Array=[];
		private var _current_notify:Number=0;
		private var _current_do_task_mc:MovieClip;
		private var _current_do_question_mc:MovieClip;
		private var _setInterval_id:Number;
		private var _timeout_id:Number;
		private var _reward:Reward;
		private var taskid : int;
		public function Home()
		{
			init();
		}
		private function init():void{
			_reward = Reward.getInstance();
			_bg= ValueObject.S1_RESOURCE.getExportedAsset("home") as MovieClip;
			addChild(_bg);
			resize();
			
			_timer = new Timer(1000);
			_service =Services.getInstance();
			_get_task_loader = new URLLoader();
			_do_task_loader = new URLLoader();
			_get_question_loader = new URLLoader();
			_do_question_loader = new URLLoader();
			getTaskList();
			notify();
			_setInterval_id = setInterval(notify,20000);
		}
		private function resize():void{
			_bg.x = ValueObject.WIDTH/2 -100;
			_bg.y = ValueObject.HEIGHT -100;
		}
		private function getTaskList():void{
			_get_task_loader.addEventListener(Event.COMPLETE,getTaskListComplete);
			_get_task_loader.addEventListener(IOErrorEvent.IO_ERROR,getTaskListError);
			_get_task_loader.load(_service.getTaskListRequest());
		}
		private function getTaskListComplete(e:Event):void{
			_get_task_loader.removeEventListener(Event.COMPLETE,getTaskListComplete);
			_get_task_loader.removeEventListener(IOErrorEvent.IO_ERROR,getTaskListError);
			_task_list_arr = JSON.decode(e.target.data);
			var leng:Number = _task_list_arr.length;
			for(var i:Number =0;i<leng;i++){
				var mc:MovieClip = _bg.getChildByName("t"+(i+1).toString()) as MovieClip;
				mc.remainTime = _task_list_arr[i].remainTime;
				if(_task_list_arr[i].remainTime <=0){
					mc.gotoAndPlay("start");
				}
			}
			if(!_timer.hasEventListener(TimerEvent.TIMER)){
				_timer.addEventListener(TimerEvent.TIMER,timerHandle);
				_timer.start();
			}
		}
		private function getTaskListError(e:IOErrorEvent):void{
			_get_task_loader.removeEventListener(Event.COMPLETE,getTaskListComplete);
			_get_task_loader.removeEventListener(IOErrorEvent.IO_ERROR,getTaskListError);
		}
		private function doTask(e:MouseEvent):void{
			if(_do_task_loader.hasEventListener(Event.COMPLETE)){
				return;
			}
			_current_do_task_mc = e.currentTarget as MovieClip;
			_do_task_loader.addEventListener(Event.COMPLETE,doTaskComplete);
			_do_task_loader.addEventListener(IOErrorEvent.IO_ERROR,doTaskError);
			_do_task_loader.load(_service.doTaskRequest(e.currentTarget.no));
			taskid = e.currentTarget.no;
		}
		private function doTaskComplete(e:Event):void{
			_do_task_loader.removeEventListener(Event.COMPLETE,doTaskComplete);
			_do_task_loader.removeEventListener(IOErrorEvent.IO_ERROR,doTaskError);
			var ob:Object = JSON.decode(e.target.data);
			switch(ob.result){
				case 0:
					break;
				case 1:
					_current_do_task_mc.gotoAndStop("wait");
					_current_do_task_mc.buttonMode = false;
					_current_do_task_mc.remainTime = ob.remainTime;
					_reward.showOn(_current_do_task_mc, SacombankScreen.getInstance().pig, _current_do_task_mc.ob);
					getTextCompleteTask(taskid);
					clearTimeout(_timeout_id);
					_timeout_id = setTimeout(notify,4000);
					if(!_timer.running){
						_timer.addEventListener(TimerEvent.TIMER,timerHandle);
						_timer.start();
					}
					break;
			}
		}
		private function doTaskError(e:IOErrorEvent):void{
			_do_task_loader.removeEventListener(Event.COMPLETE,doTaskComplete);
			_do_task_loader.removeEventListener(IOErrorEvent.IO_ERROR,doTaskError);
			trace("doTaskError");
		}
		private function getQuestion(e:MouseEvent):void{
			_current_do_question_mc = e.currentTarget as MovieClip;
			_get_question_loader.addEventListener(Event.COMPLETE,getQuestionComplete);
			_get_question_loader.addEventListener(IOErrorEvent.IO_ERROR,getQuestionError);
			_get_question_loader.load(_service.getQuestionRequest());
		}
		private function getQuestionComplete(e:Event):void{
			_get_question_loader.removeEventListener(Event.COMPLETE,getQuestionComplete);
			_get_question_loader.removeEventListener(IOErrorEvent.IO_ERROR,getQuestionError);
			var ob:Object = JSON.decode(e.target.data);
			var girl_mc:MovieClip = _bg.getChildByName(GIRL_MC) as MovieClip;
			if(ob){
				clearInterval(_setInterval_id);
				clearTimeout(_timeout_id);
				TextField(girl_mc.questionTf).text = ob.question;
				girl_mc.gotoAndStop("question");
				buttonImageInit(girl_mc.yesBtn)
				MovieClip(girl_mc.yesBtn).addEventListener(MouseEvent.CLICK,doQuestion);
				MovieClip(girl_mc.yesBtn).questionId = ob.questionId;
				buttonImageInit(girl_mc.noBtn)
				MovieClip(girl_mc.noBtn).addEventListener(MouseEvent.CLICK,doQuestion);
				MovieClip(girl_mc.noBtn).questionId = ob.questionId;
			}
			
		}
		private function getQuestionError(e:IOErrorEvent):void{
			_get_question_loader.removeEventListener(Event.COMPLETE,getQuestionComplete);
			_get_question_loader.removeEventListener(IOErrorEvent.IO_ERROR,getQuestionError);
		}
		private function doQuestion(e:MouseEvent):void{
			_do_question_loader.addEventListener(Event.COMPLETE,doQuestionComplete);
			_do_question_loader.addEventListener(IOErrorEvent.IO_ERROR,doQuestionError);
			var mc:MovieClip = e.currentTarget as MovieClip;
			switch(mc.name){
				case "yesBtn":
					_do_question_loader.load(_service.doQuestionRequest(mc.questionId,1));
					break;
				case "noBtn":
					_do_question_loader.load(_service.doQuestionRequest(mc.questionId,0));
					break;
			}
		}
		private function doQuestionComplete(e:Event):void{
			_do_question_loader.removeEventListener(Event.COMPLETE,doQuestionComplete);
			_do_question_loader.removeEventListener(IOErrorEvent.IO_ERROR,doQuestionError);
			var ob:Object = JSON.decode(e.target.data);
			var girl_mc:MovieClip = _bg.getChildByName(GIRL_MC) as MovieClip;
			MovieClip(girl_mc.yesBtn).removeEventListener(MouseEvent.CLICK,doQuestion);
			MovieClip(girl_mc.noBtn).removeEventListener(MouseEvent.CLICK,doQuestion);
			_current_do_question_mc.gotoAndStop("wait");
			_current_do_question_mc.buttonMode = false;
			_current_do_question_mc.removeEventListener(MouseEvent.CLICK,getQuestion);
			switch(ob.result){
				case 1:
					_current_do_question_mc.remainTime = ob.remainTime;
					//notify();
					_setInterval_id = setInterval(notify,6000);
					_reward.showOn(_current_do_question_mc,SacombankScreen.getInstance().pig,_current_do_question_mc.ob);
					getTextCompleteTask(4);
					break;
				case 0:
					trace("0");
				case 2:
					girl_mc.desTask.load(new URLRequest(_TASK_FOLDER +"fail.png"));
					girl_mc.gotoAndStop("notify");
					_current_do_question_mc.remainTime = ob.remainTime;
					_setInterval_id = setInterval(notify,6000);
					break;
			}
		}
		private function doQuestionError(e:IOErrorEvent):void{
			_do_question_loader.removeEventListener(Event.COMPLETE,doQuestionComplete);
			_do_question_loader.removeEventListener(IOErrorEvent.IO_ERROR,doQuestionError);
		}
		private function timerHandle(e:TimerEvent):void{
			var stop_timer:Boolean = true;
			var leng:Number = _task_list_arr.length;
			for(var i:Number=0;i<leng;i++){
				var mc:MovieClip = _bg.getChildByName("t"+(i+1).toString()) as MovieClip;
				mc.remainTime -= 1;
				mc.no = i+1;
				mc.ob = _task_list_arr[i];
				if(mc.remainTime <=0){
					mc.gotoAndPlay("start");
					if(i!=3 && !mc.hasEventListener(MouseEvent.CLICK)){
						mc.buttonMode = true;
						mc.addEventListener(MouseEvent.CLICK,doTask);
					}else if(i==3&&!mc.hasEventListener(MouseEvent.CLICK)){
						mc.buttonMode = true;
						mc.addEventListener(MouseEvent.CLICK,getQuestion);
					}
				}else{
					if(i!=3 && mc.hasEventListener(MouseEvent.CLICK)){
						mc.buttonMode = false;
						mc.removeEventListener(MouseEvent.CLICK,doTask);
					}else if(i==3&&mc.hasEventListener(MouseEvent.CLICK)){
						mc.buttonMode = false;
						mc.removeEventListener(MouseEvent.CLICK,getQuestion);
					}
					stop_timer = false;
				}
			}
			if(stop_timer){
				_timer.stop();
				_timer.removeEventListener(TimerEvent.TIMER,timerHandle);
			}
		}
		private function notify():void{
			for(var i:Number = _current_notify;i<TOTAL_TASK;i++){
				var mc:MovieClip = _bg.getChildByName("t"+(i+1).toString()) as MovieClip;
				var girl_mc:MovieClip = _bg.getChildByName(GIRL_MC) as MovieClip;
				if (mc.remainTime <= 0) {
					var task : int = i + 1;
					if (ValueObject._info_player.taskCount < 10 && ValueObject._info_player.gold < 20) {
						girl_mc.desTask.load(new URLRequest(_TASK_FOLDER + task +".png"));	
					}else {
						girl_mc.desTask.load(new URLRequest(_TASK_FOLDER + 0 +".png"));	
					}
					
					girl_mc.gotoAndStop("notify");
					_current_notify=i;
					return;
				}
			}
			_current_notify =0;
			girl_mc.gotoAndStop("stop");
			_timeout_id = setTimeout(notify,1000);
		}
		
		private function getTextCompleteTask(i:int):void {
			var girl_mc:MovieClip = _bg.getChildByName(GIRL_MC) as MovieClip;
			var task : int = i + 1;
			girl_mc.desTask.load(new URLRequest(_TASK_FOLDER +"complete/"+ i +".png"));
			girl_mc.gotoAndStop("notify");
		}
		
	}
}